/**
 * 作者: wgx
 * 日期: 2024-10-16 11:20:13
 * 备注: 用户武器表
 */
package com.motu.monstercity.server.game.userdata;

import com.motu.monstercity.protocol.Common.PBUserData;
import com.motu.monstercity.protocol.Common.PBUserWeapon;
import com.motu.monstercity.protocol.Common.PBWeaponAtt;
import com.motu.monstercity.server.game.base.ProtoDataUtils;
import com.motu.monstercity.server.game.commondata.partner.PartnerConstant;
import com.motu.monstercity.server.game.commondata.partner.PartnerMain;
import com.motu.monstercity.server.game.logic.PartnerManager;
import com.motu.vertx.module.utility.toolset.Tool;
import io.vertx.core.json.JsonArray;

public class UserWeapon extends UserWeaponModel {
    public UserWeapon() {
        super();
    }

    public void reset() {
        JsonArray jsonArray = takeAttAdd();
        JsonArray jsonArray2 = takeAttRefresh();
        boolean isUpdate = false;
        if (jsonArray2.size() != jsonArray.size()) {// 洗练结果
            for (int i=0; i<jsonArray.size(); i++) {
                if (i > jsonArray2.size() - 1) {
                    jsonArray2.add("");
                    isUpdate = true;
                }
            }
            putAttRefresh(jsonArray2.toString());
        }
        JsonArray jsonArray3 = getRefreshNumArray();// 洗练次数
        if (jsonArray3.size() != jsonArray.size()) {
            for (int i=0; i<jsonArray.size(); i++) {
                if (i > jsonArray3.size() - 1) {
                    jsonArray3.add(0);
                    isUpdate = true;
                }
            }
            putRefreshNum(jsonArray3.toString());
        }
        JsonArray jsonArray4 = getRefreshTypeArray();// 洗练类型
        if (jsonArray4.size() != jsonArray.size()) {
            for (int i=0; i<jsonArray.size(); i++) {
                if (i > jsonArray4.size() - 1) {
                    jsonArray4.add(1);
                    isUpdate = true;
                }
            }
            putRefreshType(jsonArray4.toString());
        }

        int oldAttActiveNum = getAttActiveNum();
        int newAttActiveNum = countAttActiveNum();
        if (newAttActiveNum != oldAttActiveNum) {// 属性生效的最高数量,计算成就用, 返回增加的数量
            putAttActiveNum(newAttActiveNum);
            isUpdate = true;
        }
        if (isUpdate) {
            update();
        }
    }

    public UserWeapon(long userId, int weaponId, int talent, JsonArray attAddArray) {
        super();
        putUserId(userId);
        putWeaponId(weaponId);
        putLevel(1);// 默认从1级开始
        putMaxLevel(PartnerConstant.WEAPON_INIT_MAX_LEVEL);
        putTalent(talent);
        putAwakeNum(0);// 觉醒次数
        putAttAdd(attAddArray.toString());
        JsonArray jsonArray = new JsonArray();
        for (int i=0; i<attAddArray.size(); i++) {
            jsonArray.add("");
        }
        putAttRefresh(jsonArray.toString());// 空数据,长度和属性加成一致
        putAttRefresh("");
        putIsPutOn(0);
    }

    // 武器属性有变化会影响干员战力,总战力,商业建筑的收益
    public void putAttAdd(PBUserData.Builder pbUserData, UserInfo userInfo, UserPower userPower, String attAdd) {
        super.putAttAdd(attAdd);
        if (getPartnerId() > 0) {
            UserPartner userPartner = PartnerManager.getUserPartner(userInfo.getId(), getPartnerId());
            if (userPartner != null) {
                userPartner.updateWeaponAdd(pbUserData, userInfo, userPower, this);
                userPartner.update();
                ProtoDataUtils.updatePBUserData(pbUserData, userPartner);
            }
        }
    }

    public boolean takeIsPutOn() {
        if (getIsPutOn() == 1) {
            return true;
        } else {
            return false;
        }
    }

    public JsonArray takeAttAdd() {
        if (Tool.isEmpty(getAttAdd())) {
            return new JsonArray();
        } else {
            return new JsonArray(getAttAdd());
        }
    }

    public JsonArray takeAttRefresh() {
        if (Tool.isEmpty(getAttRefresh())) {
            return new JsonArray();
        } else {
            return new JsonArray(getAttRefresh());
        }
    }

    public PBUserWeapon.Builder takeInitBuilder() {
        PBUserWeapon.Builder sb = super.takeInitBuilder();
        if (!Tool.isEmpty(getAttAdd())) {
            JsonArray jsonArray = new JsonArray(getAttAdd());
            for (int i=0;i<jsonArray.size();i++) {
                sb.addAttList(PartnerManager.takePBWeaponAtt(jsonArray.getString(i)));
            }
        }
        if (!Tool.isEmpty(getAttRefresh())) {
            JsonArray jsonArray2 = new JsonArray(getAttRefresh());
            for (int i=0;i<jsonArray2.size();i++) {
                sb.addAttListRefresh(PartnerManager.takePBWeaponAtt(jsonArray2.getString(i)));
            }
        }
        return sb;
    }

    public PBUserWeapon.Builder takeBuilder() {
        PBUserWeapon.Builder sb = super.takeBuilder();
        if (isFieldUpdate("attAdd") && !Tool.isEmpty(getAttAdd())) {
            JsonArray jsonArray = new JsonArray(getAttAdd());
            for (int i=0;i<jsonArray.size();i++) {
                sb.addAttList(PartnerManager.takePBWeaponAtt(jsonArray.getString(i)));
            }
        }
        if (isFieldUpdate("attRefresh") && !Tool.isEmpty(getAttRefresh())) {
            JsonArray jsonArray2 = new JsonArray(getAttRefresh());
            for (int i=0;i<jsonArray2.size();i++) {
                sb.addAttListRefresh(PartnerManager.takePBWeaponAtt(jsonArray2.getString(i)));
            }
        }
        return sb;
    }

    // 获取武器的洗练次数
    public int takeRefreshNum(int index) {
        try {
            return getRefreshNumArray().getInteger(index - 1);
        } catch (Exception e) {
            return 0;
        }
    }

    // 增加洗练次数
    public void addRefreshNum(int index, int addValue) {
        try {
            JsonArray list = new JsonArray();
            JsonArray jsonArray = getRefreshNumArray();
            for (int i=0;i<jsonArray.size();i++) {
                int num = jsonArray.getInteger(i);
                if (i == index - 1) {
                    num += addValue;
                }
                list.add(num);
            }
            putRefreshNum(list.toString());
        } catch (Exception e) {}
    }

//    // 当前属性生效的数量,计算成就用, 返回增加的数量, 有可能是负数,减少
//    public int reCountAttActiveNumAdd() {
//        int oldAttActiveNum = getAttActiveNum();// 原来的生效数量
//        int newAttActiveNum = CountAttActiveNum();// 现在的生效数量
//        int addValue = newAttActiveNum - oldAttActiveNum;
//        putAttActiveNum(newAttActiveNum);
//        return addValue;
//    }

    // 计算武器技能的生效数量
    public int countAttActiveNum() {
        if (getPartnerId() == 0) {
            return 0;
        }
        PartnerMain partnerMain = PartnerManager.getPartnerMain(getPartnerId());
        if (partnerMain == null) {
            return 0;
        }
        int total = 0;
        JsonArray jsonArray = takeAttAdd();
        for (int i = 0; i < jsonArray.size(); i++) {
            PBWeaponAtt.Builder pbAtt = PartnerManager.takePBWeaponAtt(jsonArray.getString(i));
            if (pbAtt.getType() == partnerMain.getCharacterType()) {
                total++;
            }
        }
        return total;
    }

    // 武器等级上限
    public int getMaxLevel(UserPower userPower) {
        return maxLevel + userPower.getWeaponLevelMaxAdd();
    }
}
